/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MpmProjectProductOutInfoService
* 外部产品信息表
*
* 修改纪录
* 2021-11-25 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2021-11-25
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.topscomm.mpm.pojo.*;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectProductOutInfoService;
import com.topscomm.mpm.service.impl.auto.MpmProjectProductOutInfoServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectProductOutInfoService")
@Scope(value = "singleton")
public class MpmProjectProductOutInfoService extends MpmProjectProductOutInfoServiceAuto implements IMpmProjectProductOutInfoService {
	@Autowired
	private MpmProjectProductOutInfoHisService mpmProjectProductOutInfoHisService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removeAndAddRecordForProductOut(Map<String, Object> paraMap) {
		if (!paraMap.containsKey("productoutlist") && paraMap.get("productoutlist") == null) {
			return;
		}
		// 删除原有记录,生成新记录
		this.deleteByWhere(" projectid = '" + ConvertUtil.convertToString(paraMap.get("id")) + "' ");
		JSONArray jsonArray = (JSONArray) paraMap.get("productoutlist");
		List<Map> list = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
		if (CollectionUtil.isEmpty(list)) {
			return;
		}
		for (Map product : list) {
			MpmProjectProductOutInfoEntity mpmProjectProductInfoEntity = new MpmProjectProductOutInfoEntity();
			mpmProjectProductInfoEntity.convertFromMap(product);
			mpmProjectProductInfoEntity.setId(0L);
			mpmProjectProductInfoEntity.setProjectid(ConvertUtil.convertToLong(paraMap.get("id")));
			mpmProjectProductInfoEntity.setProjectname(ConvertUtil.convertToString(paraMap.get("projectname")));
			mpmProjectProductInfoEntity.setProjectcode(ConvertUtil.convertToString(paraMap.get("projectcode")));
			this.insert(mpmProjectProductInfoEntity);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-04-24
	 * @description: 变更完成后同步子表内容
	 * @modify:
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateFromHistory(long id) {
		// 获取变更记录
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" changeid = '{0}' ", id);
		List<MpmProjectProductOutInfoHisEntity> hisEntities = mpmProjectProductOutInfoHisService.queryByWhere(sql.toString());
		if (CollectionUtil.isEmpty(hisEntities)) {
			return;
		}
		// 变更完成后同步子表内容
		long projectid = hisEntities.get(0).getProjectid();
		this.deleteByWhere(" projectid = '" + projectid + "' ");
		for (MpmProjectProductOutInfoHisEntity tempHisEntity : hisEntities) {
			MpmProjectProductOutInfoEntity productEntity = new MpmProjectProductOutInfoEntity();
			productEntity.convertFromMap(tempHisEntity.convertToMap());
			productEntity.setId(0L);
			this.insert(productEntity);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-04-21
	 * @description: 将productoutrange字段同步到productoutinfo子表
	 * @modify:
	 */
	@Transactional(rollbackFor = Exception.class)
	public void insertSubProductOutInfo(Map<String, Object> mpmProjectMap) {
		// 获取产品信息
		List<Map> list = JSONObject.parseArray(ConvertUtil.convertToString(mpmProjectMap.get("productoutrange")), Map.class);
		if (CollectionUtil.isEmpty(list)) {
			return;
		}
		for (Map tempMap : list) {
			MpmProjectProductOutInfoEntity productOutInfoEntity = new MpmProjectProductOutInfoEntity();
			productOutInfoEntity.setFund(ConvertUtil.convertToBigDecimal(tempMap.get("productOutMoney")));
			productOutInfoEntity.setProductname(ConvertUtil.convertToString(tempMap.get("productOutType")));
			productOutInfoEntity.setProjectcode(ConvertUtil.convertToString(mpmProjectMap.get("projectcode")));
			productOutInfoEntity.setProjectname(ConvertUtil.convertToString(mpmProjectMap.get("projectname")));
			productOutInfoEntity.setProjectid(ConvertUtil.convertToLong(mpmProjectMap.get("id")));
			this.insert(productOutInfoEntity);
		}
	}
}
